<?php
/**
 * RoomBooking View for RoomBooking Component
 * 
 * @version 1.0
 * @package    Joomla
 * @subpackage RoomBooking
 * @copyright (C) 2011 Giovanni Marzulli
 * @license		GNU/GPL
*/
 
// no direct access
 
defined( '_JEXEC' ) or die( 'Restricted access' );
 
jimport( 'joomla.application.component.view');

class RoomBookingViewBookings extends JView
{
	/**
     * RoomBooking view display method
     * @return void
     **/
    function display($tpl = null)
    {
    	$mainframe = &JFactory::getApplication();
        
                        //organization list
                        $organization_list = $this->get('OrganizationList');
                        $defaultOpt = new stdClass();
                        $defaultOpt->id = 0;
                        $defaultOpt->name = '- '.JText::_( 'すべてのユーザ' ).' -';
                        array_push($organization_list, $defaultOpt);
                        
                        $organization_id = JRequest::getVar('id_organization');
                        if($organization_id == null||$organization_id == 0)$organization_id = $organization_list[0]->id;
                        
                        $organization_select = JHTML::_('select.genericlist', $organization_list, 'id_organization', 'class=" inputbox"', 'id', 'name', $organization_id);
                        $this->assignRef( 'organization_select', $organization_select );
                        
                        //building list
                        $building_list = $this->get('BuildingList');
                        $defaultOpt = new stdClass();
                        $defaultOpt->id = 0;
                        $defaultOpt->name = '- '.JText::_( 'すべてのユーザ' ).' -';
                        array_push($building_list, $defaultOpt);
                        
                        $building_id = JRequest::getVar('id_building');
                        if($building_id == null||$building_id == 0)$building_id = $this->get('IdBuilding');
                        if($building_id == 0)$building_id = $building_list[0]->id;
                        
                        $building_select = JHTML::_('select.genericlist', $building_list, 'id_building', 'class=" inputbox"', 'id', 'name', $building_id);
                        $this->assignRef( 'building_select', $building_select );
                        
                        //room list
                        $room_list = $this->get('RoomList');
//                        echo $room_list[0]->id;
                        $defaultOpt = new stdClass();
                        $defaultOpt->id = 0;
                        $defaultOpt->name = '- '.JText::_( 'すべてのユーザ' ).' -';
                        array_push($room_list, $defaultOpt);
                        
                        $room_id = JRequest::getVar('id_room');
                        if($room_id == null || $room_id == 0)$room_id = $room_list[0]->id;
                        
                        $room_select = JHTML::_('select.genericlist', $room_list, 'id_room', 'class=" inputbox"', 'id', 'name', $room_id);
                        $this->assignRef( 'room_select', $room_select );
        
    		$id_room=JRequest::getVar('id_room');
                if($id_room == null||$id_room == 0)$id_room = $room_list[0]->id;
			if (is_numeric($id_room)){
				$modelRoom = &$this->getModel('room');
				if($roomName = $modelRoom->getRoomName($id_room)){
				$modelBookings = &$this->getModel('bookings');
				$id_building = $modelRoom->getIdBuildOfRoom($id_room);
				$modelBuilding =& $this->getModel('building');
				$times = $modelBuilding->getBuildOpenCloseTime($id_building);
				$minTime = $times->opening;
				$maxTime = $times->closing;
				$this->assignRef( 'maxTime', $maxTime );
				$this->assignRef( 'minTime', $minTime );
				$show_WE = $modelBuilding->getWEOpening($id_building);
				$this->assignRef( 'show_WE', $show_WE );
				$bookings = $modelBookings->getBookings($id_room);
				$this->assignRef( 'bookings', $bookings );
				$this->assignRef( 'roomName', $roomName );
				$this->assignRef( 'id_room', $id_room );
			}
			else
				$this->setLayout('notify');
			}
			else
				$mainframe->redirect('index.php?option=com_roombooking&view=roombooking');
                        
	
      	  parent::display($tpl);
    }
    
	 /**
	 * Method to get name Building of a room
	 * @return	string
	 */
	function getBuildOfRoom($id_room) {
		$modelRoom =& $this->getModel('room');
		$modelBuilding =& $this->getModel('building');
		$id_building = $modelRoom->getIdBuildOfRoom($id_room);
        $build = $modelBuilding->getBuildName($id_building);
        
		return $build;
	}
    
    
}
